package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.textileexpes.beans.ReceiveAddressBean;
import com.itboye.textileexpes.presenter.ReceiveAddressPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.TextUtility;
import com.itboye.textileexpres.widget.SwipeMenu;
import com.itboye.textileexpres.widget.SwipeMenuCreator;
import com.itboye.textileexpres.widget.SwipeMenuItem;
import com.itboye.textileexpres.widget.SwipeMenuListView;
import com.itboye.textileexpres.widget.SwipeMenuListView.OnMenuItemClickListener;
import com.itboye.textileexpres.widget.XListView.IXListViewListener;
/**
 * 收货地址
 *
 */
public class ActivityAddressManage extends BaseActivity implements
		OnClickListener, Observer, IXListViewListener {
	// ListView list;
	MyAddressAdapter adapter;
	// RelativeLayout add_rel;
	ImageView back;
	TextView title;
	ImageView img_other;
	ReceiveAddressPresenter addressPresenter;
	ArrayList<ReceiveAddressBean> beans = new ArrayList<ReceiveAddressBean>();
	String address_id;
	SwipeMenuListView list;
	TextView delete;
	ReceiveAddressBean model = new ReceiveAddressBean();
	String type;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_address_manage;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		title.setText(R.string.myAddress);
		img_other.setVisibility(View.GONE);
		View view = LayoutInflater.from(this).inflate(R.layout.item_addaddress,
				null);
	
		addressPresenter = new ReceiveAddressPresenter(this);
		addressPresenter.queryAddress(IsMobileNumPhone.isUid());
		list.addFooterView(view);
		type = getIntent().getStringExtra("type");
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityAddressManage.this,
						ActivityReceiptAddress.class);
				startActivity(intent);
			}
		});
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				SwipeMenuItem deleteItem = new SwipeMenuItem(
						ActivityAddressManage.this);
				deleteItem.setBackground(new ColorDrawable(getResources()
						.getColor(R.color.red)));
				deleteItem.setTitle(getResources().getString(R.string.delete) + "");
				deleteItem.setTitleColor(Color.WHITE);
				deleteItem.setWidth(TextUtility.dp2px(90));
				deleteItem.setTitleSize(18);
				menu.addMenuItem(deleteItem);
			}
		};
		list.setMenuCreator(creator);
		list.setXListViewListener(this);
		list.setPullLoadEnable(true);
		list.setPullRefreshEnable(true);
		list.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				// TODO Auto-generated method stub
				model.setId(beans.get(position).getId());
				model.setUid(IsMobileNumPhone.isUid());
				addressPresenter.delete(model);
			}
		});
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		addressPresenter.queryAddress(IsMobileNumPhone.isUid());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == ReceiveAddressPresenter.QuerySuccessEvent) {
				// beans = (List<ReceiveAddressBean>) entity.getData();
				beans.clear();
				beans.addAll((ArrayList<ReceiveAddressBean>) entity.getData());
				adapter = new MyAddressAdapter(this, beans);
				list.setAdapter(adapter);
				list.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub

						Intent intent = new Intent();
						intent.putExtra("name", beans.get(arg2 - 1)
								.getContactname() + "");
						intent.putExtra("isdefault", beans.get(arg2 - 1)
								.getisDefault() + "");
						intent.putExtra("phone", beans.get(arg2 - 1)
								.getMobile() + "");
						intent.putExtra("countryid", beans.get(arg2 - 1)
								.getcountryId());
						intent.putExtra(
								"address", beans.get(arg2 - 1).getProvince()
										+ beans.get(arg2 - 1).getArea()+ "");
						intent.putExtra("detailinfo", beans.get(arg2 - 1)
								.getDetailinfo() + "");
						intent.putExtra("postcode", beans.get(arg2 - 1)
								.getpostalCode() + "");
						intent.putExtra("id", beans.get(arg2 - 1).getId() + "");
						intent.putExtra("provice", beans.get(arg2 - 1)
								.getProvince() + "");
						intent.putExtra("proviceid", beans.get(arg2 - 1)
								.getProvinceid() + "");
						intent.putExtra("city", beans.get(arg2 - 1).getCity()
								+ "");
						intent.putExtra("cityid", beans.get(arg2 - 1)
								.getCityid() + "");
						intent.putExtra("area", beans.get(arg2 - 1).getArea()
								+ "");
						intent.putExtra("areaid", beans.get(arg2 - 1)
								.getAreaid() + "");
						intent.putExtra("country", beans.get(arg2 - 1)
								.getCountry() + "");
						address_id = beans.get(arg2 - 1).getId() + "";
						System.out.println("tiantian"
								+ beans.get(arg2 - 1).getCountry()
								+ "countryid"
								+ beans.get(arg2 - 1).getcountryId() + "city"
								+ beans.get(arg2 - 1).getCity() + "cityid"
								+ beans.get(arg2 - 1).getCityid() + "area"
								+ beans.get(arg2 - 1).getArea() + "areaid"
								+ beans.get(arg2 - 1).getAreaid());

						if ("goods_detail".equals(type)) {

							setResult(102, intent);
							finish();
						} else {
							intent.setClass(ActivityAddressManage.this,
									ActivityAddressUpdate.class);
							startActivity(intent);
						}

					}
				});
				stop();
			} else if (entity.getEventType() == ReceiveAddressPresenter.QueryFailureEvent) {
				toast(entity.getData() + "");
			}
			if (entity.getEventType() == ReceiveAddressPresenter.DeleteSuccessEvent) {
				toast(entity.getData() + "");
				addressPresenter.queryAddress(IsMobileNumPhone.isUid());
				adapter.notifyDataSetChanged();
			}

		} else {

		}
	}

	void stop() {
		list.stopLoadMore();
		list.stopRefresh();
	}

	class MyAddressAdapter extends BaseAdapter {
		Context context;
		List<ReceiveAddressBean> lists;

		public MyAddressAdapter(Context context) {
			// TODO Auto-generated constructor stub
			this.context = context;
		}

		public MyAddressAdapter(Context context, List<ReceiveAddressBean> lists) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.lists = lists;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lists.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return lists == null ? 0 : lists.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			viewHolder holder = null;
			if (convertView == null) {
				holder = new viewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_address_manage, null);
				holder.detail_address = (TextView) convertView
						.findViewById(R.id.detail_address);
				holder.phone = (TextView) convertView.findViewById(R.id.phone);
				holder.default_tv = (TextView) convertView
						.findViewById(R.id.default_tv);
				holder.address = (TextView) convertView
						.findViewById(R.id.address);
				convertView.setTag(holder);
			} else {
				holder = (viewHolder) convertView.getTag();
			}
			if (lists.get(position).getisDefault() == "1"
					|| "1".equals(lists.get(position).getisDefault())) {
				holder.default_tv.setVisibility(View.VISIBLE);
			} else {
				holder.default_tv.setVisibility(View.GONE);
			}
			holder.detail_address.setText(lists.get(position).getContactname());
			holder.phone.setText(lists.get(position).getMobile());
			holder.address.setText(lists.get(position).getProvince()
					+ lists.get(position).getArea()
					+ lists.get(position).getDetailinfo());
			return convertView;
		}

	}

	class viewHolder {
		TextView detail_address, phone, default_tv, address;
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub

	}

}
